package ${packageName}.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
<#list entityMappers as item>
import ${packageName}.entity.${item.mapperName};
</#list>
<#list entityMappers as item>
import ${packageName}.mapper.${item.mapperName}Mapper;
</#list>
import ${packageName}.dto.${entityDTO};
import ${packageName}.dto.${entityMultiDTO};
import ${packageName}.service.I${entityName}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import ${packageCoreName}.common.lang.StringToolUtils;
import java.util.*;

/**
* @author OFG
* @version 1.0
* @date 2021/1/6 20:09
* @Description
*/
@Service
public class I${entityName}ServiceImpl implements I${entityName}Service {
<#list entityMappers as item>
	@Autowired
	private ${item.mapperName}Mapper ${item.mapperVar}Mapper;

</#list>
	@Transactional(readOnly = false,rollbackFor = Exception.class,propagation= Propagation.REQUIRED)
	@Override
	public void insertEntity(${methodArgs}) {
	<#list entityMappers as item>
		<#if item.relIdFlag==false>
		${item.mapperVar}Mapper.insert(${item.mapperVar});

			<#else>
		${item.mapperVar}.set${item.relId}(${item.relArgs}.get${pkId}());
		${item.mapperVar}Mapper.insert(${item.mapperVar});

		</#if>
	</#list>
	}

	@Transactional(readOnly = false,rollbackFor = Exception.class,propagation= Propagation.REQUIRED)
	@Override
	public void deletePrimaryKey(Integer id) {
	<#list entityMappers as item>
		<#if item.relIdFlag==true>
		QueryWrapper<${item.entityName}> queryWrapper${item_index} = new QueryWrapper<>();
		queryWrapper${item_index}.eq("${item.orgRelId}",id);
		${item.mapperVar}Mapper.delete(queryWrapper${item_index});

		<#else>
		${item.mapperVar}Mapper.deleteById(id);
		</#if>
	</#list>
	}

	@Transactional(readOnly = false,rollbackFor = Exception.class,propagation= Propagation.REQUIRED)
	@Override
	public void updateEntity(${methodArgs}) {
	<#list entityMappers as item>
		<#if item.relIdFlag==false>
		${item.mapperVar}Mapper.updateById(${item.mapperVar});

		<#else>
		QueryWrapper<${item.entityName}> queryWrapper${item_index} = new QueryWrapper<>();
		queryWrapper${item_index}.eq("${item.orgRelId}",${entityNameVar}.get${pkId}());
		${item.mapperVar}Mapper.update(${item.mapperVar},queryWrapper${item_index});
		</#if>
	</#list>
	}

	@Override
	public ${entityDTO} selectByPrimaryKey(Integer id) {

		return ${entityNameVar}Mapper.selectMultiJoinById(id);
	}

	@Override
	public IPage<${entityMultiDTO}> selectEntityList(Page<${entityMultiDTO}> page,${entityMultiDTO} record) {
		Map<String,Object> params = new HashMap<>();
		<#list sqlArgs as item>
			<#if item.argsType=='IN'>
			List<String> list = Arrays.asList(record.${item.argsValue}.split(","));
			params.put("${item.argsKey}",list.isEmpty()?null:list);
			<#else>
			params.put("${item.argsKey}",StringToolUtils.isEmptyObj(record.${item.argsValue})?null:record.${item.argsValue});
			</#if>
		</#list>
		return ${entityNameVar}Mapper.selectEntityList(page,params);
	}


}
